package br.imr.support;
 

import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
import static javax.faces.application.FacesMessage.SEVERITY_INFO;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 */
public abstract class FacesBeanSupport {
	 
 
	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

    protected HttpServletResponse getResponse() {
        return (HttpServletResponse) getFacesContext().getExternalContext().getResponse();
    }

    protected HttpServletRequest getRequest() {
        return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
    }

    protected ServletContext getServletContext() {
        return (ServletContext) getFacesContext().getExternalContext().getContext();
    }
    
    public String getParameter(String name) {
        return getRequest().getParameter(name);
    }

    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    public ResourceBundle getBundle() {
        return ResourceBundle.getBundle(getBundleName(), getRequest().getLocale());
    }

    public String getBundleName() {
        return getFacesContext().getApplication().getMessageBundle();
    }
    
    public String getMessage(String key) {
        try {
            return getBundle().getString(key);
        } catch (java.util.MissingResourceException mre) { 
            return "";
        }
    }

    public String getMessage(String key, String ... arg) {
        MessageFormat form = new MessageFormat(getMessage(key));
        return form.format(arg);
    }

	protected void addMessage(String key, String args) {
        FacesMessage facesMessage = new FacesMessage(SEVERITY_INFO, key, args);   
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);  
    }
	protected void addMessage(String key) {
        FacesMessage facesMessage = new FacesMessage(SEVERITY_INFO, key, key);   
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);  
    }

	protected void addError(String key) {
        FacesMessage facesMessage = new FacesMessage(SEVERITY_ERROR, key, null);   
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);   
    }
	protected void addError(String key, String args) {
        FacesMessage facesMessage = new FacesMessage(SEVERITY_ERROR, key, args);   
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);   
    }
 
//	
//	protected Long getCodUsuario() { 
//		Usuario user = getUsuario();
//		return user == null ? null: user.getCodigo(); 
//	}
//
//	
//	protected String getNomeUsuario() { 
//		Usuario user = getUsuario();
//		return user == null ? null: user.getNome(); 
//	}
//	
//	protected Usuario getUsuario() { 
//		Usuario user = (Usuario) getSession().getAttribute("usuario");
//		return user ; 
//	}
	
	public void reset() {
	}
 
}
